//
//  HerosViewController.m
//  LOLBOX
//
//  Created by lanou3g on 15/12/17.
//  Copyright © 2015年 张明. All rights reserved.
//

#import "HerosViewController.h"
#import "HerosViewCollectionViewCell.h"
#import "HeroModel.h"
#import "HeroHelper.h"
#import "Segmented.h"
#define heroURL @"http://lolbox.duowan.com/phone/apiHeroes.php?type=all&v=108&OSType=iOS8.3"
#define myHeroURL @"http://lolbox.duowan.com/phone/apiMyHeroes.php?serverName=网通六&OSType=iOS8.3&target=明信念&v=108"
#define MFHeroURL @"http://lolbox.duowan.com/phone/apiHeroes.php?type=free&v=108&OSType=iOS8.3"
#import "UIImageView+WebCache.h"
#import "KVNProgressConfiguration.h"
#import "KVNProgress.h"
#import "HerosNextFatherViewController.h"
@interface HerosViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UISearchResultsUpdating>
//创建collectionView
@property(nonatomic,strong)UICollectionView *collectionView;
//创建搜索栏
@property(nonatomic,strong)UISearchController *searchController;
@property(nonatomic,strong)UISegmentedControl *segment;
//小菊花
@property(nonatomic,strong)KVNProgressConfiguration *kVn;
@end

@implementation HerosViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.166 green:0.185 blue:0.455 alpha:1.000]];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:17]};
   //注册herosCell
    [self createCollectionView];
    [self.collectionView registerClass:[HerosViewCollectionViewCell class] forCellWithReuseIdentifier:@"herocell"];
    [self createSegment];
     [self.segment addTarget:self action:@selector(setAction:) forControlEvents:UIControlEventValueChanged];
}
-(void)createSegment{
    NSArray *array = @[@"我的英雄",@"免费英雄",@"所有英雄"];
    self.segment = [[UISegmentedControl alloc] initWithItems:array];
    self.segment.frame = CGRectMake(0, self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, 30);
    self.segment.tintColor = [UIColor blackColor];
    [self.view addSubview:self.segment];
}

- (void)setAction:(UISegmentedControl *)sender{
    if(sender.selectedSegmentIndex ==0) {
        self.kVn.fullScreen = YES;
        [KVNProgress showWithStatus:@"玩命加载中..."];
        NSString *url = [NSString stringWithFormat:myHeroURL];
        url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
        [[HeroHelper shareHeroWithHelpData]getHeroDataWithUrl:url Key:@"myHeroes" successData:^(NSMutableArray *array) {
            [self.collectionView reloadData];
            NSLog(@"我的英雄请求成功");
            [KVNProgress dismiss];
        } fail:^(NSError *err) {
            NSLog(@"我的英雄请求失败");
        }];
        
    }else if (sender.selectedSegmentIndex ==1){
        self.kVn.fullScreen = YES;
        [KVNProgress showWithStatus:@"玩命加载中..."];
        [[HeroHelper shareHeroWithHelpData]getHeroDataWithUrl:MFHeroURL Key:@"free" successData:^(NSMutableArray *array) {
            [self.collectionView reloadData];
            NSLog(@"周免英雄请求成功");
            [KVNProgress dismiss];
        } fail:^(NSError *err) {
            NSLog(@"周免英雄请求失败");
        }];
    }else if (sender.selectedSegmentIndex ==2) {
        self.kVn.fullScreen = YES;
        [KVNProgress showWithStatus:@"玩命加载中..."];
        [[HeroHelper shareHeroWithHelpData]getHeroDataWithUrl:heroURL Key:@"all" successData:^(NSMutableArray *array) {
            [self.collectionView reloadData];
            NSLog(@"所有英雄请求成功");
            [KVNProgress dismiss];
        } fail:^(NSError *err) {
            NSLog(@"数据请求是失败");
        }];
        
    }
    
    
}
#pragma mark -- 创建collectionView
-(void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 15;
    layout.minimumInteritemSpacing = 5;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(self.view.frame.size.width*3/7, self.view.frame.size.height*0.09);
    layout.sectionInset = UIEdgeInsetsMake(10, 15,10 , 15);
    //  layout.headerReferenceSize = CGSizeMake(0, self.view.frame.size.height *0.1);
    // layout.footerReferenceSize = CGSizeMake(0, 65);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, CGRectGetHeight(self.view.frame) - 60 - self.navigationController.navigationBar.frame.size.height - self.tabBarController.tabBar.frame.size.height) collectionViewLayout:layout];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    
    [self.view addSubview:self.collectionView];
}
#pragma  mark - collectionView datasouce

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [HeroHelper shareHeroWithHelpData].allDataMutableArr.count;;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HerosViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"herocell" forIndexPath:indexPath];
    HeroModel *model = [HeroHelper shareHeroWithHelpData].allDataMutableArr[indexPath.row];
    cell.nameLable.text = model.title;
    cell.secondLable.text = model.cnName;
    cell.typeLable.text = model.location;
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/champions/%@_120x120.jpg",model.enName]] placeholderImage:[UIImage imageNamed:@"load.jpg"]];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
   
    HerosNextFatherViewController *hernextVc = [[HerosNextFatherViewController alloc] init];
    HeroModel *model =  [HeroHelper shareHeroWithHelpData].allDataMutableArr[indexPath.row];
    hernextVc.name = model.enName;
    hernextVc.price = model.price;
    hernextVc.heroName = model.title;
    hernextVc.nextName = model.cnName;
    hernextVc.location = model.location;
    
    [self.navigationController pushViewController:hernextVc animated:YES];
    [self.searchController.searchBar removeFromSuperview];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
